<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>拖拽事件</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      #box {
        height: 250px;
        width: 250px;
        background-color: #000;
        position: absolute;
      }
    </style>
  </head>
  <body>
    <div id="box"></div>
    <script>
      let boxEle = document.getElementById('box')

      boxEle.addEventListener('mousedown', function (event) {

        let downh = event.clientY - boxEle.getBoundingClientRect().top
        let downx = event.clientX - boxEle.getBoundingClientRect().left

        let move = function (event) {
          let moveX = event.clientX
          let moveY = event.clientY
          boxEle.style.left = moveX - downx  + 'px'
          boxEle.style.top = moveY - downh + 'px'
        }
        document.addEventListener('mousemove', move)
        this.addEventListener('mouseup',function(){
            document.removeEventListener('mousemove', move)
        })
      })
    </script>
  </body>
</html>
